package com.hanlink.dsp.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.edge.pbxdsp.R;
import com.qztech.utils.YzDebug;

/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class ClipRoundLinearLayout extends LinearLayout {
    int radiusX = 26, radiusY = 26, offset = 4;
    private Path mPath;
    public ClipRoundLinearLayout(Context context) {
        this(context, null);
    }

    public ClipRoundLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClipRoundLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPath = new Path();
        if (attrs != null){
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ClipRoundLinearLayout);
            radiusX = array.getDimensionPixelSize(R.styleable.ClipRoundLinearLayout_crll_radiusX,radiusX);
            radiusY = array.getDimensionPixelSize(R.styleable.ClipRoundLinearLayout_crll_radiusY,radiusY);
            array.recycle();
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
//        YzDebug.d("onSizeChanged " + w +" " + h);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
//        YzDebug.d("onLayout "  + l +" " + t+" " + r+" " + b);
        mPath.reset();
        mPath.addRoundRect(new RectF(offset ,offset, r-l-offset, b-t-offset), radiusX, radiusY, Path.Direction.CW);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.save();
        canvas.clipPath(mPath);
        super.dispatchDraw(canvas);
        canvas.restore();
//        YzDebug.d("dispatchDraw");
    }
}
